package com.knowif.backend.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.knowif.backend.dto.OfferDto;
import com.knowif.pojo.*;
import com.knowif.service.*;
import com.knowif.system.model.RestBean;
import com.knowif.util.commonUtil.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019/6/5 0005 20:58
 */
@Service
public class OfferService {

    @Autowired
    private Offer_service offer_service;
    @Autowired
    private OfferInterviewThanksCollection_service offerInterviewThanksCollectionService;
    @Autowired
    private OfferInterviewComment_service offerInterviewCommentService;
    @Autowired
    private User_service tUserService;
    @Autowired
    private StanderSorce_service standerSorceService;

    /**
     * 条件查询offer
     *
     * @param offerExample
     * @return
     */
    public List<Offer> selectOffer(OfferExample offerExample) {
        return offer_service.selectOffer(offerExample);
    }

    /**
     * 条件查询offer
     *
     * @param offerExample
     * @param pageable     是否需要分页
     * @return
     */
    public List<Offer> selectOffer(OfferExample offerExample, boolean pageable) {
        if (!pageable) {
            PageHelper.clearPage();
        }
        return offer_service.selectOffer(offerExample);
    }

    /**
     * 保存offer
     *
     * @return
     */
    public boolean saveOffer(Offer offer) {
        int i = offer_service.saveOffer(offer);
        if (i == 0) {
            return false;
        }
        return true;
    }

    /**
     * 保存offer
     *
     * @return
     */
    public boolean updateOfferNotNull(Offer offer) {
        int i = offer_service.updateOfferNotNull(offer);
        if (i == 0) {
            return false;
        }
        return true;
    }

    /**
     * 保存offer
     *
     * @return
     */
    public boolean updateOffer(Offer offer) {
        int i = offer_service.updateOffer(offer);
        if (i == 0) {
            return false;
        }
        return true;
    }

    /**
     * 删除offer
     *
     * @return
     */
    public boolean deleteOffer(int offerId) {
        int i = offer_service.deleteOffer(offerId);
        if (i == 0) {
            return false;
        }
        return true;
    }

    /**
     * 主键查询
     *
     * @param id
     * @return
     */
    public Offer selectById(int id) {
        return offer_service.selectOfferById(id);
    }

    public RestBean<?> detail(int offerId, int userId) {
        Offer offer = offer_service.selectOfferById(offerId);
        OfferExample offerExample = new OfferExample();
        //只要一个
        offerExample.createCriteria().andIdEqualTo(offerId);
        List<Offer> offers = offer_service.selectOffer(offerExample);
        if (CollectionUtils.isEmpty(offers)) {
            return RestBean.buildError("offer不存在");
        }
        OfferDto offerDto = new OfferDto();
        offerDto.setOffers(offers);
        offerDto.setOffer(offer);
        TUser tUser = tUserService.selectById(offer.getUserId());
        offerDto.setTUser(tUser);
//        StanderSorceExample standerSorceExample = new StanderSorceExample();
//        standerSorceExample.createCriteria().andUserIdEqualTo(offer.getUserId());
//        List<StanderSorce> standerSorces = standerSorceService.selectStanderSorce(standerSorceExample);
        Integer standerScoreId = offer.getStanderScoreId();
        if (null != standerScoreId) {
            StanderSorce standerSorce = standerSorceService.selectStanderSorceById(standerScoreId);
            offerDto.setStanderSorce(standerSorce);
        }
        //感谢收藏类型1感谢2收藏
        OfferInterviewThanksCollectionExample thanksExample = new OfferInterviewThanksCollectionExample();
        thanksExample.createCriteria().andCollectionTypeEqualTo((byte) 1)//1offer2面经
                .andThanksCollectionTypeEqualTo(1)//1感谢2收藏
                .andOfferInterviewIdEqualTo(offerId).andUserIdEqualTo(userId);
        int thanks = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(thanksExample);

        OfferInterviewThanksCollectionExample collectionExample = new OfferInterviewThanksCollectionExample();
        collectionExample.createCriteria().andCollectionTypeEqualTo((byte) 1)//1offer2面经
                .andThanksCollectionTypeEqualTo(2)//1感谢2收藏
                .andOfferInterviewIdEqualTo(offerId).andUserIdEqualTo(userId);
        int collections = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(collectionExample);

        OfferInterviewCommentExample offerInterviewCommentExample = new OfferInterviewCommentExample();
        offerInterviewCommentExample.createCriteria()
                .andCommentTypeEqualTo(1).andParentIdIsNull().andOfferInterviewIdEqualTo(offerId);
        ;
        offerInterviewCommentExample.setOrderByClause("comment_time DESC");
        List<OfferInterviewComment> offerInterviewComments = offerInterviewCommentService.selectOfferInterviewComment(offerInterviewCommentExample);

        JSONObject result = new JSONObject();
        result.put("offer", offerDto);
        result.put("isThanks", thanks > 0 ? true : false);
        result.put("isCollections", collections > 0 ? true : false);
        result.put("comments", offerInterviewComments);

        return RestBean.buildSuccess(result);
    }
}
